# File lib/patch.rb, line 4 def initialize(resources, conf) stealables = [:area,:max_bands] stealables.each{|s|self.instance_variable_set("@#{s.to_s}".to_sym, conf[s.to_s])} @resources = resources.map{|type,cc| Object.const_get(type).new(cc, area)} @num_bands = 0 end
# File lib/patch.rb, line 37 def depleted? @resources.all? { |r| r.depleted? } end
# File lib/patch.rb, line 33 def do_time_step @resources.each { |r| r.grow } end
serves a join attempt; returns success status
# File lib/patch.rb, line 13 def join if joinable? @num_bands += 1 return true end false # Patch full, no room for more end
# File lib/patch.rb, line 21 def joinable? @num_bands < @max_bands or @max_bands == 0 end
# File lib/patch.rb, line 25 def leave if @num_bands <= 0 warn "Error: a band attempted to leave a patch with no bands" return end @num_bands -= 1 end
# File lib/patch.rb, line 48 def to_s resources = "[" << @resources.map(&:to_s).reduce { |m, o| "#{m}, #{o}" } << "]" # Try that in Java "Patch (id:#{object_id}) { Bands: #{@num_inhabitants}/#{@max_bands} | Resources:#{resources} }" end
does it have a viable population for the specified resource type?
# File lib/patch.rb, line 42 def viable?(resource) found = @resources.find { |r| r.name == resource } return found.viable? if found false end